﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using GrayParrot.Web.Controls;
using GrayParrot.Core.Filters;
using FimetContoDeposito.DAL;
using FimetContoDeposito.BLL.Objects;
using System.Data.Entity.Core.Objects;
using System.Data.Entity;

namespace FimetContoDeposito.BLL.ResultSets
{
    public class MovimentiResultSet : ResultSet<Cronologia>
	{
        public MovimentiResultSet(DTParameters param, IQueryable<Cronologia> result)
            : base(param, result)
        {
        }

        public override Expression<Func<Cronologia, bool>> ApplyGeneralFilter(string searchPattern)
        {
            var predicate = PredicateBuilder.False<Cronologia>();

            if (!string.IsNullOrWhiteSpace(searchPattern))
            {
                predicate = predicate.Or(im => im.NumeroDDT != null && im.NumeroDDT.ToString().Contains(searchPattern));
                predicate = predicate.Or(im => im.Cliente != null && im.Cliente.ToString().Contains(searchPattern));
                predicate = predicate.Or(im => im.DataDDT != null && im.DataDDT.ToString().Contains(searchPattern));
                predicate = predicate.Or(im => im.TipoMovimento != null && im.TipoMovimento.ToString().Contains(searchPattern));
            }

            return predicate;
        }

        public override Expression<Func<Cronologia, bool>> ApplyGeneralColumnsFilter(List<string> columnSearch)
        {
            var predicate = PredicateBuilder.True<Cronologia>();
            GrayParrot.Web.Controls.Datatables.Filters.DateRange m_dates = null;

            string m_id = columnSearch[0];
            string m_numeroDDT = columnSearch[1];
            DateTime m_dataDDT = default(DateTime);
            if (columnSearch[2] != null)
            {
                m_dates = GrayParrot.Web.Controls.Datatables.Filters.DateFilterHelper.GetDateValue(columnSearch[2]);
            }
            string m_idCliente = columnSearch[3];
            string m_cliente = columnSearch[4];
            string m_tipoMovimento = columnSearch[5];

            if (!string.IsNullOrWhiteSpace(m_numeroDDT))
            {
                predicate = predicate.And(im => im.NumeroDDT.Contains(m_numeroDDT));
            }

            if (m_dates != null)
            {
                if (m_dates.IsRange)
                {
                    // aggiungi al filtro il controllo sulla data...
                    predicate = predicate.And(im => (DbFunctions.TruncateTime(im.DataDDT) >= DbFunctions.TruncateTime(m_dates.Start)) &&
                                                     DbFunctions.TruncateTime(im.DataDDT) <= DbFunctions.TruncateTime(m_dates.End));
                }
                else
                {
                    predicate = predicate.And(im => im.DataDDT.Equals(m_dates.Start));
                }
            }

            //if (m_dataDDT != default(DateTime))
            //{
            //    predicate = predicate.And(im => im.DataDDT.Equals(m_dataDDT));
            //}

            if (!string.IsNullOrWhiteSpace(m_cliente))
            {
                predicate = predicate.And(im => im.Cliente.Contains(m_cliente));
            }

            if (!string.IsNullOrWhiteSpace(m_tipoMovimento))
            {
                TipoMovimento value = (TipoMovimento)Enum.Parse(typeof(TipoMovimento), m_tipoMovimento);
                predicate = predicate.And(im => (int)im.TipoMovimento == (int)value);
            }

            return predicate;
        }
	}
}
